#include "formsettings.h"
#include "formmain.h"
#include "definitions.h"
#include <QDir>
#include <QFileDialog>
#include <QStyleFactory>


FormSettings::FormSettings(QSettings *settings, QWidget *parent) : QDialog(parent), ui(new Ui::FormSettings) {
    ui->setupUi(this);
    this->settings = settings;
    connect(ui->btnOk, SIGNAL(clicked()), this, SLOT(saveSettings()));
    connect(ui->btnBrowser, SIGNAL(clicked()), this, SLOT(selectBrowser()));
    connect(ui->btnCustomPath, SIGNAL(clicked()), this, SLOT(selectCustomFile()));
    connect(ui->txtBrowser, SIGNAL(textChanged(QString)), this, SLOT(checkBrowser()));
    connect(ui->radioUseBrowser, SIGNAL(toggled(bool)), this, SLOT(enableBrowser()));

    // nastav formulář
    FormMain::checkSettings(this->settings);
    ui->checkMinimized->setChecked(this->settings->value("startMinimized", false).toBool());
    ui->checkInterval->setChecked(this->settings->value("updateAutomatically", true).toBool());
    ui->spinInterval->setValue(this->settings->value("updateInterval", 1800000).toInt() / 60000);
    ui->checkStartup->setVisible(false);
    ui->checkOpenBrowserRightClick->setChecked(settings->value("rightClickbrowser", false).toBool());

    QDir myDir(":/lang/lang");
    ui->listLanguage->addItem("English");
    ui->listLanguage->addItems(myDir.entryList());

    QList<QListWidgetItem*> list = ui->listLanguage->findItems(this->settings->value("currentLanguage", "English").toString(), Qt::MatchExactly);
    ui->listLanguage->setCurrentItem(list.at(0));
    ui->txtBrowser->setText(this->settings->value("browser", "").toString());
    ui->checkBrowser->setChecked(this->settings->value("useBrowser", ui->checkBrowser->isChecked()).toBool());

    ui->radioUseTab->setChecked(this->settings->value("useNewTab", true).toBool());
    ui->radioUseBrowser->setChecked(!this->settings->value("useNewTab", true).toBool());

    ui->radioOpenWebsite->setChecked(this->settings->value("openWebsite", true).toBool());
    ui->radioOpenSimple->setChecked(!this->settings->value("openWebsite", true).toBool());

    ui->spinTimeout->setValue(settings->value("updateTimeout", DEF_TIMEOUT).toInt());

    ui->checkShowCounts->setChecked(settings->value("unreadCounts", false).toBool());

    if (settings->value("feedFileNamePath", CONFIG_UNKNOWN).toString() != CONFIG_UNKNOWN) {
        ui->btnCustomPath->setEnabled(true);
        ui->txtCustomPath->setEnabled(true);
        ui->checkCustomPath->setChecked(true);
        ui->txtCustomPath->setText(settings->value("feedFileNamePath", CONFIG_UNKNOWN).toString());
    }

    if (!ui->radioUseTab->isChecked()) {
        ui->radioOpenSimple->setEnabled(false);
        ui->radioOpenWebsite->setEnabled(false);
    }

    ui->checkDeleteOnExit->setChecked(settings->value("deleteReadOnExit", false).toBool());

#if defined(Q_WS_WIN)
    ui->checkStartup->setVisible(true);
    QSettings sett("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
    if (sett.value("RSSGuard", "").toString().replace('\\', '/') == QApplication::applicationFilePath())
        ui->checkStartup->setChecked(true);
#endif
}

FormSettings::~FormSettings() {
    delete ui;
}

void FormSettings::enableBrowser() {
    if (ui->radioUseBrowser->isChecked()) {
        ui->checkBrowser->setEnabled(true);
    }
    else {
        ui->checkBrowser->setEnabled(false);
    }
}

void FormSettings::selectBrowser() {
    QString filterEnd;
#if defined(Q_WS_X11) || defined(Q_WS_MAC)
    filterEnd = " (*);;";
#endif
#if defined(Q_WS_WIN)
    filterEnd = " (*.exe);;";
#endif
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select browser"), QDir::homePath(),
                                                    tr("Executable files") + filterEnd);
    if (fileName != NULL)
        ui->txtBrowser->setText(fileName);
}

void FormSettings::selectCustomFile() {
    QString fileName = QFileDialog::getSaveFileName(this, tr("Select custom feeds file"), QDir::homePath(),
                                                    tr("All files(*.*);;XML files(*.xml);;RSS Guard XML (*.rgx);;"));
    if (!fileName.size()) return;
    if (fileName != NULL)
        ui->txtCustomPath->setText(fileName);
}

void FormSettings::checkBrowser() {
    if (QFile::exists(ui->txtBrowser->text())) {
        ui->lblBrowserExist->setStyleSheet("color: green;");
        ui->lblBrowserExist->setText(tr("  Selected file exists."));
    }
    else {
        ui->lblBrowserExist->setStyleSheet("color: red;");
        ui->lblBrowserExist->setText(tr("  Selected file doesn't exist."));
    }
}

void FormSettings::saveSettings() {
    settings->setValue("startMinimized", ui->checkMinimized->isChecked());
    settings->setValue("updateAutomatically", ui->checkInterval->isChecked());
    settings->setValue("updateInterval", ui->spinInterval->value() * 60000);
    if (ui->listLanguage->currentItem())
        settings->setValue("currentLanguage", ui->listLanguage->currentItem()->text());
    settings->setValue("browser", ui->txtBrowser->text());
    settings->setValue("useBrowser", ui->checkBrowser->isChecked());
    settings->setValue("rightClickbrowser", ui->checkOpenBrowserRightClick->isChecked());

    settings->setValue("useNewTab", ui->radioUseTab->isChecked());
    settings->setValue("openWebsite", ui->radioOpenWebsite->isChecked());

    settings->setValue("updateTimeout", ui->spinTimeout->value());
    settings->setValue("unreadCounts", ui->checkShowCounts->isChecked());

    settings->setValue("deleteReadOnExit", ui->checkDeleteOnExit->isChecked());

    if (ui->checkCustomPath->isChecked())
        settings->setValue("feedFileNamePath", ui->txtCustomPath->text());
    else
        settings->setValue("feedFileNamePath", CONFIG_UNKNOWN);

#ifdef Q_WS_WIN
    QSettings sett("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
    if (ui->checkStartup->isChecked())
        sett.setValue("RSSGuard", QApplication::applicationFilePath().replace('/', '\\'));
    else
        sett.setValue("RSSGuard", "");
#endif

    FormMain::checkSettings(settings);
}
